// pages/message/addressBook/addressBook.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    classList: [],
    stuList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let _this = this
    _this.setData({
      people: wx.getStorageSync("people")
    })

    if (wx.getStorageSync("myCode") != '' && wx.getStorageSync("myCode") != null && app.globalData.teacher_info['id'] != null && wx.getStorageSync("people") == 'teacher') {
      app.formGet('/get_all_class_by_teacher_id?teacher_id=' + app.globalData.teacher_info['id']).then(res => {
        for (let i = 0; i < res.length; i++) {
          res[i]['text'] = res[i]['courseName']
        }
        console.log('老师获取班级列表：',res);
        this.setData({
          classList: res,
        });
      })
    }

    if (wx.getStorageSync("myCode") != '' && wx.getStorageSync("myCode") != null && app.globalData.student_info['id'] != null && wx.getStorageSync("people") == 'student') {
      app.formPost('/get_all_class_by_student_id?', {
        'student_id': app.globalData.student_info['id']
      }).then(res => {
        for (let i = 0; i < res.length; i++) {
          res[i]['text'] = res[i]['className']
        }
        // console.log(res);
        this.setData({
          classList: res,
        });
      })
    }

  },

  getBanji: function (e) {
    console.log(e.detail)
    let course_name = e.detail['text']
    app.formGet('/get_points_sort?course_name=' + course_name).then(res => {
      // console.log(res);
      this.setData({
        stuList: res
      })
      console.log('获取班级学生列表：',this.data.stuList);
    })
  },

  //添加好友、创建会话
  talk: function (e) {
    console.log(e);
    app.globalData.chat_info['receiver_id'] = e.target.dataset['id']
    app.globalData.chat_info['receiver_identity'] = 0
    let data1 = {
      'friendid': app.globalData.chat_info['receiver_id'],
      'identity': app.globalData.chat_info['receiver_identity'],
      'identity_user': app.globalData.chat_info['sender_identity'],
      'userid': app.globalData.chat_info['sender_id']
    }
    app.formPost('/add_fridend?', data1).then(res => {
      console.log('添加好友:', res);
    })
    let data2 = {
      'friendid': app.globalData.chat_info['sender_id'],
      'identity': app.globalData.chat_info['sender_identity'],
      'identity_user': app.globalData.chat_info['receiver_identity'],
      'userid': app.globalData.chat_info['receiver_id'],
    }
    app.formPost('/add_fridend?', data2).then(res => {
      console.log('添加好友:', res);
    })
    //创建会话
    let data3 = {
      'chatid': app.globalData.chat_info['receiver_id'],
      'identity': app.globalData.chat_info['receiver_identity'],
      'identity_user': app.globalData.chat_info['sender_identity'],
      'userid': app.globalData.chat_info['sender_id']
    }
    app.formPost('/add_conversation?', data3).then(res => {
      console.log('创建会话:', res);
    })
    let data4 = {
      'chatid': app.globalData.chat_info['sender_id'],
      'identity': app.globalData.chat_info['sender_identity'],
      'identity_user': app.globalData.chat_info['receiver_identity'],
      'userid': app.globalData.chat_info['receiver_id'],
    }
    app.formPost('/add_conversation?', data4).then(res => {
      console.log('创建会话:', res);
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})